{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python数据运算\n",
    "\n",
    "算术运算/比较运算/赋值运算/逻辑运算/成员运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1 - 算术运算\n",
    "常用算术运算符：加法+、减法-、乘法、除法/、取模%、幂指数*、取整//。 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|运算符| 描述 | 返回 | 实例 |\n",
    "|:--:| :--:| :-----------: |:-----:|\n",
    "|+ |加 |相加结果| a+b; >>> 13|\n",
    "|- | 减 | 相减结果 |  a-b; >>> 5|\n",
    "|*  |乘 |相乘结果 |  a*b; >>> 36 |\n",
    "|/ |  除 | 相除结果| a/b; >>> 2.25|\n",
    "|% |取模| 除法余数| a%b; >>> 1 |\n",
    "|**|幂 |a的b次幂  | a**b; >>> 6561|\n",
    "|// |   取整除|    商的整数 |  a//b;>>> 2|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6561"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=9\n",
    "b=4\n",
    "a**b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2 - 比较运算\n",
    "常用算术运算符：加法+、减法-、乘法、除法/、取模%、幂指数*、取整//。 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|运算符| 描述|  返回 | 实例 |\n",
    "|:--:| :--:| :------: |:-----:|\n",
    "|== |      等于   |   是否相等    |  a==b; >>> False |\n",
    "|!= |    不等于 |    是否不相等  |    a!=b; >>> True |\n",
    "|<>    |   不等于 |   是否不相等   |   a<>b; >>> True|\n",
    "|>     |   大于    |  是否大于     |   a>b; >>> True|\n",
    "|<   | 小于     | 是否小于      | a< b;  >>> False|\n",
    "|(>=)   |  大于等于  |是否大于等于   | a>=b; >>> True|\n",
    "|<=    |   小于等于 | 是否小于等于  |  a<=b; >>> False|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=9\n",
    "b=4\n",
    "a!=b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3 - 赋值运算\n",
    "将一些数据赋值给自定义的变量，对数据运算的中间结果进行存储，以备后续使用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|运算符  |   描述    |    实例|\n",
    "|:--:| :--:| :------: |\n",
    "|=    |   简单赋值  |  c=a+b; 将a+b赋值为c|\n",
    "|+    |    加法赋值  |  c+=a; 等效于c=c+a|\n",
    "|--   |   减法赋值   | c-=a; 等效于c=c-a|\n",
    "|*    |    乘法赋值  |  c×=a; 等效于c=c×a|\n",
    "|/    |    除法赋值  |  c/=a; 等效于c=c/a|\n",
    "|%    |    取模赋值  |  c%=a; 等效于c=c%a|\n",
    "|**  |     幂赋值    |  c**=a; 等效于c=c^a|\n",
    "|// |     取整除赋值 | c//=a; 等效于c=c//a|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=9\n",
    "b=4\n",
    "c=0\n",
    "c=a+b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "22"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c+=a\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c-=a\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c%=a\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "c//=a\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4 - 逻辑运算\n",
    "逻辑运算所涉及的数据类型为布尔值，返回值仅仅存在有限的几种可能性。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 运算符|       描述     |                                                         实例|\n",
    "|:--:| :--:| :------: |\n",
    "|and      |    与, 如果a为False，a and b返回False，否则它返回b的值。    |        a and b 返回 4\n",
    "|or      |     或，如果a是非0，他返回a的值，否则它返回b的值。            |       a or b 返回 9\n",
    "|not    |      非，如果a为 True，返回 False 。如果a为False它返回True。    |      not a 返回 False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "变量 a 和 b 都为 True，或其中一个变量为 True\n"
     ]
    }
   ],
   "source": [
    "a=9\n",
    "b=4\n",
    "if a or b:\n",
    "    print (\"变量 a 和 b 都为 True，或其中一个变量为 True\")\n",
    "else:\n",
    "    print (\"变量 a 和 b 都为 False\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "变量 a 和 b 都为 True\n"
     ]
    }
   ],
   "source": [
    "if not(a and b):\n",
    "    print (\"变量 a 和 b 都为 false，或其中一个变量为 false\")\n",
    "else:\n",
    "    print (\"变量 a 和 b 都为 True\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5 - 成员运算\n",
    "为复杂的数据结构而设立的一种运算，主要面向元组、列表、字典。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|运算符|    描述|\n",
    "|:---:|:---:|\n",
    "|in     |    如果在指定的序列中找到值返回 True，否则返回 False。\n",
    "|not in  |   如果在指定的序列中找到值返回 True，否则返回 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "变量 a 在给定的列表中 list 中\n"
     ]
    }
   ],
   "source": [
    "a=9\n",
    "b=4\n",
    "list=[1,3,5,7,9]\n",
    "if ( a in list ):\n",
    "    print (\"变量 a 在给定的列表中 list 中\")\n",
    "else:\n",
    "    print (\"变量 a 不在给定的列表中 list 中\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "变量 b 不在给定的列表中 list 中\n"
     ]
    }
   ],
   "source": [
    "if (b not in list):\n",
    "    print (\"变量 b 不在给定的列表中 list 中\")\n",
    "else:\n",
    "    print (\"变量 b 在给定的列表中 list 中\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
